package cn.yitun.chat.yitunserverrunsingleboot.startup;

import cn.yitun.chat.yitunservercommonlib.databind.jackson.CustomLocalDateDeserializer;
import cn.yitun.chat.yitunservercommonlib.databind.jackson.CustomLocalDateSerializer;
import cn.yitun.chat.yitunservercommonlib.databind.jackson.CustomLocalDateTimeDeserializer;
import cn.yitun.chat.yitunservercommonlib.databind.jackson.CustomLocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * jackson自定义
 * Description 
 * <br>
 * Date 2019-04-29 09:16:42<br>
 * @author yitun
 * @version 1.1
 */
@Configuration
public class WebJsonConfig {
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer initJackson() {
        Jackson2ObjectMapperBuilderCustomizer customizer = new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder builder) {
                // 自定义LocalDateTime类型转换器
                builder.deserializerByType(LocalDateTime.class, new CustomLocalDateTimeDeserializer());
                builder.serializerByType(LocalDateTime.class, new CustomLocalDateTimeSerializer());
                // 自定义LocalDate类型转换器
                builder.deserializerByType(LocalDate.class, new CustomLocalDateDeserializer());
                builder.serializerByType(LocalDate.class, new CustomLocalDateSerializer());
                // 不包含null的字段
                // builder.serializationInclusion(JsonInclude.Include.NON_NULL);
                // 不包含空字符串字段
                // builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
            }
        };
        return customizer;
    }
}
